﻿package mortal.game.manager
{
    import Message.Public.*;
    import baseEngine.system.*;
    import com.gengine.global.*;
    import com.gengine.keyBoard.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.cd.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.skillProgress.*;
    import mortal.game.view.test.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GameManager extends Object
    {
        private var _showHideNum:int = 0;
        public var lastOperTime:int = 0;
        private var _testPanel:TestPanel;
        private static var _instance:GameManager = new GameManager;

        public function GameManager()
        {
            if (_instance != null)
            {
                throw new Error("GameManager 单例");
            }
            return;
        }// end function

        public function updateLastOperTime() : void
        {
            this.lastOperTime = getTimer();
            return;
        }// end function

        public function init() : void
        {
            KeyBoardManager.instance.addEventListener(KeyBoardManager.ALTKEYCHANGE, this.altKeyChange);
            KeyBoardManager.instance.addEventListener(KeyEvent.KEY_DOWN, this.onKeyDownHandler);
            KeyBoardManager.instance.addEventListener(KeyEvent.KEY_UP, this.onKeyUpHandler);
            KeyBoardManager.instance.start();
            this.updateLastOperTime();
            return;
        }// end function

        private function altKeyChange(event:Event) : void
        {
            ThingUtil.isHeadContainerChange = true;
            return;
        }// end function

        private function onKeyUpHandler(event:KeyEvent) : void
        {
            var _loc_3:Object = null;
            var _loc_2:* = event.keyEvent.keyCode;
            if (_loc_2 == KeyCode.D)
            {
                KeyBoardCd.keyUp(_loc_2);
            }
            else if (event.keyData != null && event.keyData.keyMap != null)
            {
                _loc_3 = event.keyData.keyMap.key;
                if (_loc_3 >= 1 && _loc_3 <= 20 || _loc_3 == ModuleType.PickBox)
                {
                    if (_loc_3 == ModuleType.PickBox)
                    {
                        _loc_3 = GameConst.PickupKeyCode;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarKeyUp, _loc_3));
                }
            }
            return;
        }// end function

        private function onKeyDownHandler(event:KeyEvent) : void
        {
            if (event.keyEvent.target is TextField)
            {
                if (event.keyEvent.target.type == TextFieldType.INPUT)
                {
                    return;
                }
            }
            if (event.keyEvent.target is TextInput)
            {
                return;
            }
            if (this.hasKeyCodeEvent(event.keyEvent.keyCode, event) == false)
            {
                if (event.keyData.keyMap)
                {
                    this.KeyEventHandler(event.keyData.keyMap.key, event);
                }
            }
            return;
        }// end function

        private function hasKeyCodeEvent(param1:uint, param2:KeyEvent) : Boolean
        {
            var _loc_3:Boolean = false;
            if (param1 == KeyCode.N)
            {
                if (Global.isDebugModle)
                {
                    if (!this._testPanel)
                    {
                        this._testPanel = new TestPanel();
                    }
                    this.popupView(this._testPanel);
                }
            }
            if (Global.isDebugModle)
            {
            }
            if (param1 == KeyCode.F2 || param1 == KeyCode.F && KeyBoardManager.ShiftKey)
            {
                if (Global.isDebugModle)
                {
                    this.popupView(DebugWindow.instance);
                    return true;
                }
            }
            else if (param1 == KeyCode.ESCAPE)
            {
                if (LayerManager.isHasFullSceenOpen())
                {
                    PopupManager.closeAllFullView();
                }
                else
                {
                    _loc_3 = PopupManager.closeTopWindow();
                    if (!_loc_3)
                    {
                        if (ThingUtil.selectEntity)
                        {
                            ThingUtil.selectEntity = null;
                        }
                        else
                        {
                            GameManager.instance.popupModuleType(ModuleType.SysSet);
                        }
                    }
                }
                return true;
            }
            else if (Global.isDebugModle && param1 == KeyCode.G && KeyBoardManager.ShiftKey)
            {
                WizardScreenPlayer.instance.play("180061001");
                return true;
            }
            return false;
        }// end function

        private function KeyEventHandler(param1:Object, param2:KeyEvent) : void
        {
            if (param1 == ModuleType.Test)
            {
                if (Global.isDebugModle)
                {
                }
            }
            else if (param1 >= 1 && param1 <= 20 || param1 == ModuleType.PickBox)
            {
                if (LayerManager.isHasFullSceenOpen())
                {
                    return;
                }
                if (param1 == ModuleType.PickBox)
                {
                    param1 = GameConst.PickupKeyCode;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarKeyDown, param1));
            }
            else
            {
                this.popupModuleType(param1);
            }
            return;
        }// end function

        private function debug() : void
        {
            return;
        }// end function

        public function popupModuleType(param1:Object) : void
        {
            switch(param1)
            {
                case ModuleType.Players:
                {
                    if (GameController.playerSystem.isViewShow)
                    {
                        this.popupView(GameController.playerSystem.view);
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PlayerWinOpen));
                    }
                    break;
                }
                case ModuleType.DrugPack:
                {
                    this.popupView(GameController.drugPack.view);
                    break;
                }
                case ModuleType.Vip:
                {
                    this.popupView(GameController.vip.view);
                    break;
                }
                case ModuleType.Pack:
                {
                    this.popupView(GameController.pack.view);
                    break;
                }
                case ModuleType.Warehouse:
                {
                    this.popupView(GameController.warehouse.view);
                    break;
                }
                case ModuleType.Wizard:
                {
                    if (!GameController.autoGuide.hasGuideWizard)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20462, GameDefConfig.instance.getSystemOpenLevel(5)));
                        return;
                    }
                    this.popupView(GameController.wizard.view);
                    break;
                }
                case ModuleType.ShopMall:
                {
                    this.popupView(GameController.shopMall.view);
                    break;
                }
                case ModuleType.Shops:
                {
                    this.popupView(GameController.shop.view);
                    break;
                }
                case ModuleType.GroupAvatar:
                {
                    this.popupView(GameController.groupAvatarController.view);
                    break;
                }
                case ModuleType.Group:
                {
                    this.popupView(GameController.group.view);
                    break;
                }
                case ModuleType.Skills:
                {
                    if (!GameController.autoGuide.hasShowModule(ModuleType.Skills))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20464));
                        return;
                    }
                    this.popupView(GameController.skill.view);
                    break;
                }
                case ModuleType.Tasks:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideModule));
                    break;
                }
                case ModuleType.Pets:
                {
                    if (LevelCheckUtil.isSystemOpen(LevelCheckUtil.pet) == false)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20465));
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetNewPopUpWin));
                    break;
                }
                case ModuleType.Daily:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DailyOpenByIndex));
                    break;
                }
                case ModuleType.Mounts:
                {
                    if (GameController.autoGuide.hasShowModule(ModuleType.Mounts))
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20467, GameDefConfig.instance.getSystemOpenLevel(1)));
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MountPopupWin));
                    break;
                }
                case ModuleType.Guild:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildOpen));
                    break;
                }
                case ModuleType.Issm:
                {
                    this.popupView(GameController.issm.view);
                    break;
                }
                case ModuleType.GM:
                {
                    this.popupView(GameController.GmComplain.view);
                    break;
                }
                case ModuleType.Friend:
                {
                    if (Cache.instance.role.entityInfo.level < GameConst.FriendOpenLevel)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20467, GameConst.FriendOpenLevel));
                        return;
                    }
                    this.popupView(GameController.friend.view);
                    break;
                }
                case ModuleType.Build:
                {
                    if (!GameController.autoGuide.hasShowModule(ModuleType.Build))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20468));
                        return;
                    }
                    this.popupView(GameController.forging.view);
                    break;
                }
                case ModuleType.Market:
                {
                    if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(15))
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20469, GameDefConfig.instance.getSystemOpenLevel(15)));
                        return;
                    }
                    this.popupView(GameController.market.view);
                    break;
                }
                case ModuleType.Trade:
                {
                    this.popupView(GameController.trade.view);
                    break;
                }
                case ModuleType.SmallMap:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapShowHide));
                    break;
                }
                case ModuleType.Mail:
                {
                    if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(31))
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20470, GameDefConfig.instance.getSystemOpenLevel(31)));
                        return;
                    }
                    this.popupView(GameController.mail.view);
                    break;
                }
                case ModuleType.SysSet:
                {
                    this.popupView(GameController.systemSetting.view);
                    break;
                }
                case ModuleType.AutoFightView:
                {
                    this.popupView(GameController.autoFight.view);
                    break;
                }
                case ModuleType.Ranking:
                {
                    if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(12))
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20471, GameDefConfig.instance.getSystemOpenLevel(12)));
                        return;
                    }
                    this.popupView(GameController.ranking.view);
                    break;
                }
                case ModuleType.LoginNotice:
                {
                    this.popupView(GameController.loginNotice.view);
                    break;
                }
                case ModuleType.SignUp:
                {
                    if (Cache.instance.role.entityInfo.level < GameConst.SignUpOpenLevel)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20472, GameConst.SignUpOpenLevel));
                        return;
                    }
                    this.popupView(GameController.signUp.view);
                    break;
                }
                case ModuleType.FashionOnline:
                {
                    this.popupView(GameController.fashionOnline.view);
                    break;
                }
                case ModuleType.CopySystem:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyOpenByIndex));
                    break;
                }
                case ModuleType.Custom:
                {
                    break;
                }
                case ModuleType.Achievement:
                {
                    this.popupView(GameController.achievementController.view);
                    break;
                }
                case ModuleType.Arena:
                {
                    break;
                }
                default:
                {
                    this.popupOtherType(param1);
                    return;
                    break;
                }
            }
            return;
        }// end function

        private function popupOtherType(param1:Object) : void
        {
            var _loc_2:int = 0;
            var _loc_3:ICDData = null;
            var _loc_4:SkillInfo = null;
            var _loc_5:int = 0;
            var _loc_6:GameScene3D = null;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            var _loc_12:int = 0;
            var _loc_13:Number = NaN;
            var _loc_14:Number = NaN;
            if (LayerManager.isHasFullSceenOpen())
            {
                return;
            }
            switch(param1)
            {
                case ModuleType.Enter:
                {
                    (GameController.chat.view as ChatModule).setChatInputFocus();
                    KeyBoardManager.instance.changeImeEnable(true);
                    break;
                }
                case ModuleType.RUN:
                {
                    if (!BuffUtil.isCanRoleWalk())
                    {
                        return;
                    }
                    if (Cache.instance.skill.isWaittingForServer(GameConst.JPSkillId))
                    {
                        return;
                    }
                    if (Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeChangeBody))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20473));
                        return;
                    }
                    if (GameMapUtil.curMapState.isSunbathMap)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20474));
                        return;
                    }
                    if (GameMapUtil.curMapState.isWineMap)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20475));
                        return;
                    }
                    if (!GameController.autoGuide.hasGuideJP())
                    {
                        _loc_2 = GameDefConfig.instance.getSystemOpenLevel(26);
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20476, _loc_2));
                        return;
                    }
                    if (Cache.instance.role.roleInfo.stamina >= GameConst.TConstSprint)
                    {
                        _loc_3 = Cache.instance.cd.getCDData(Cache.instance.skill.getSkill(GameConst.JPSkillId));
                        if (_loc_3 != null && _loc_3.isCoolDown)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20477));
                            return;
                        }
                        Cache.instance.skill.isLastUseRolling = false;
                        Cache.instance.skill.setWaittingForServer(GameConst.JPSkillId);
                        GameProxy.sceneProxy.sprint();
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.RoleShowStaminaFilter));
                        MsgManager.showFightTipsImpl(Language.getString(20478), MsgRollTipsType.msgRollTips6, true);
                    }
                    break;
                }
                case ModuleType.SPACE:
                {
                    _loc_4 = Cache.instance.skill.getSkill(GameConst.RollingSkillId);
                    if (_loc_4 == null || !BuffUtil.isCanRoleFireSkill(_loc_4))
                    {
                        return;
                    }
                    if (Cache.instance.skill.isWaittingForServer(GameConst.RollingSkillId))
                    {
                        return;
                    }
                    if (Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeChangeBody))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20479));
                        return;
                    }
                    if (GameMapUtil.curMapState.isSunbathMap)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20480));
                        return;
                    }
                    if (GameMapUtil.curMapState.isWineMap)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20481));
                        return;
                    }
                    if (!GameController.autoGuide.hasGuideRolling())
                    {
                        _loc_5 = GameDefConfig.instance.getSystemOpenLevel(25);
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20482, _loc_5));
                        return;
                    }
                    _loc_3 = Cache.instance.cd.getCDData(Cache.instance.skill.getSkill(GameConst.RollingSkillId));
                    if (_loc_3 != null && _loc_3.isCoolDown)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20477));
                        return;
                    }
                    if (Cache.instance.role.roleInfo.stamina >= GameConst.TConstSomersaultExpend)
                    {
                        if (AutoFightAI.isWorking)
                        {
                            (AIManager.curAI as AutoFightAI).setPlayerRolling();
                        }
                        _loc_6 = GameScene3D(Global3D.scene);
                        _loc_7 = GameCamera(_loc_6.camera).x2d;
                        _loc_8 = GameCamera(_loc_6.camera).y2d;
                        _loc_9 = Global.stage.mouseX + _loc_7;
                        _loc_10 = Global.stage.mouseY + _loc_8;
                        _loc_11 = MathUitl.getRadiansByXY(RolePlayer.instance.x2d, RolePlayer.instance.y2d, _loc_9, _loc_10);
                        _loc_12 = GameConst.somersaultDistance;
                        _loc_13 = RolePlayer.instance.x2d + (_loc_12 - 1) * Math.cos(_loc_11);
                        _loc_14 = RolePlayer.instance.y2d + (_loc_12 - 1) * Math.sin(_loc_11);
                        Dispatcher.dispatchEvent(new DataEvent(EventName.SceneSomersault, new Point(_loc_13, _loc_14)));
                        Cache.instance.skill.setWaittingForServer(GameConst.RollingSkillId);
                        Cache.instance.skill.isLastUseRolling = true;
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.RoleShowStaminaFilter));
                        MsgManager.showFightTipsImpl(Language.getString(20484), MsgRollTipsType.msgRollTips6, true);
                    }
                    break;
                }
                case ModuleType.SwitchTarget:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TabKeyChangeTarget));
                    break;
                }
                case ModuleType.AutoFight:
                {
                    if (GameController.arena.isMatchSucc || GameMapUtil.curMapState.isArenaCopy)
                    {
                        break;
                    }
                    if (GameController.copy.heroArenaCopy.isMatchSucc || GameMapUtil.curMapState.isHeroArenaMap)
                    {
                        break;
                    }
                    if (AutoFightAI.isWorking && !AutoFightAI.instance.isInAroundFight)
                    {
                        AIManager.cancelAll();
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFight_A));
                    break;
                }
                case ModuleType.RangeAutoFight:
                {
                    if (GameController.arena.isMatchSucc || GameMapUtil.curMapState.isArenaCopy)
                    {
                        break;
                    }
                    if (GameController.copy.heroArenaCopy.isMatchSucc || GameMapUtil.curMapState.isHeroArenaMap)
                    {
                        break;
                    }
                    if (AutoFightAI.isWorking && AutoFightAI.instance.isInAroundFight)
                    {
                        AIManager.cancelAll();
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFight_Round));
                    break;
                }
                case ModuleType.DownUpMounts:
                {
                    if (RolePlayer.instance.isJumping)
                    {
                        break;
                    }
                    if (!GameController.autoGuide.hasGuideMount())
                    {
                        break;
                    }
                    if (GameMapUtil.curMapState.isArenaCopy || GameMapUtil.curMapState.isHeroArenaMap)
                    {
                        MsgManager.showMouseMsg(Language.getString(20485), MsgRollTipsType.msgRollTips2);
                        break;
                    }
                    if (GameController.arena.isMatchSucc || GameController.copy.heroArenaCopy.isMatchSucc)
                    {
                        break;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MountRide));
                    break;
                }
                case ModuleType.PetsRest:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetOutOrInByShortCut));
                    break;
                }
                case ModuleType.PickBox:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.KeyPickDropItem));
                    break;
                }
                case ModuleType.XP:
                {
                    if (!GameController.autoGuide.hasGuideWizard)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20486, GameDefConfig.instance.getSystemOpenLevel(5)));
                        return;
                    }
                    if (GameMapUtil.curMapState.isPetBreakMap)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(70345));
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WizardUseXpSkill));
                    break;
                }
                case ModuleType.Rest:
                {
                    if (RolePlayer.instance.isInSwinArea)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20487));
                    }
                    else if (SkillProgressView.isInProgress)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20488));
                    }
                    else if (RolePlayer.instance.entityInfo.entityInfo.level < GameConst.OpenZazenLevel)
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20489, GameConst.OpenZazenLevel));
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.AskSingleZazen));
                    }
                    break;
                }
                case ModuleType.Pay:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function getCopyId(param1:int) : int
        {
            var _loc_2:* = param1.toString().substring(2);
            return int(_loc_2);
        }// end function

        public function popupView(param1:IView) : void
        {
            if (param1.isHide)
            {
                PopupManager.closeAllFullView();
                param1.show();
            }
            else
            {
                param1.hide();
            }
            return;
        }// end function

        public static function get instance() : GameManager
        {
            return _instance;
        }// end function

    }
}
